package org.lanqiao.controller.information;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.lanqiao.config.entity.ResponseData;
import org.lanqiao.information.Comment;
import org.lanqiao.information.ICommentService;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/information/comment")
@Api(tags = "评论Comment模块API")
public class CommentController {
    @Reference
    ICommentService commentService;

    @PostMapping("/add")
    @ApiOperation(value = "求助帖新增评论", notes = "回复某一个资源帖")
    @ApiImplicitParam(name = "comment", value = "评论comment实体", required = true, paramType = "query", dataTypeClass = Comment.class)
    public ResponseData<Comment> add(@RequestBody Comment comment) {
        try {
            comment.setCommentTime(new Date());
            commentService.add(comment);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/logicallyDelete")
    @ApiOperation(value = "根据id逻辑删除评论", notes = "逻辑删除某一个评论")
    @ApiImplicitParam(name = "comId", value = "评论的主键comId", required = true, paramType = "query", dataTypeClass = Comment.class)
    public ResponseData<Comment> logicallyDelete(@RequestParam Integer comId) {
        try {
            commentService.logicallyDelete(comId);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }


    @GetMapping("/physicallyDelete")
    @ApiOperation(value = "根据id物理删除评论", notes = "物理删除某一个评论")
    @ApiImplicitParam(name = "comId", value = "评论实体的主键comId", required = true, paramType = "query", dataTypeClass = Comment.class)
    public ResponseData<Comment> physicallyDelete(@RequestParam Integer comId) {
        try {
            commentService.physicallyDelete(comId);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/findAll")
    @ApiOperation(value = "查找所有评论", notes = "查找数据库中的所有评论")
    public ResponseData<Comment> findAll() {
        try {
            // 实际应该从session中获取用户的id 然后使用用户的id查询

            List<Comment> commentList = commentService.findAll();
            System.out.println(commentList);
            return ResponseData.success().putDataVule("commentList", commentList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/findAll1")
    @ApiOperation(value = "查找所有评论", notes = "查找数据库中的所有评论")
    public ResponseData<Comment> findAll(@RequestParam Integer userId) {
        try {
            List<Comment> commentList = commentService.findAll(userId);
            return ResponseData.success().putDataVule("commentList", commentList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/pageAll")
    @ApiOperation(value = "分页查找所有评论", notes = "分页查找数据库中的所有评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class)
    })
    public ResponseData<Comment> pageAll(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        try {
            IPage<Comment> page = commentService.pageAll(pageNum, pageSize);
            return ResponseData.success().putDataVule("commentList", page.getRecords()).putDataVule("pages", page.getPages()).putDataVule("total", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "分页条件查找评论", notes = "分页条件查找数据库中的评论，至少包含一个条件," +
            "条件为comId,content,status,informUserId,informContent,infoId,userId,orderby")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataTypeClass = Map.class)
    })
    public ResponseData<Comment> pageList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Map<String, String> queryMap) {
        try {
            IPage<Comment> page = commentService.pageList(pageNum, pageSize, queryMap);
            return ResponseData.success().putDataVule("commentList", page.getRecords()).putDataVule("pages", page.getPages()).putDataVule("total", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @PostMapping("/pageListByUserId")
    @ApiOperation(value = "分页根据用户查找评论", notes = "分页条件根据用户id查找数据库中的评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataType = "map")
    })
    public ResponseData<Comment> pageListByUserId(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Map<String, String> queryMap) {
        try {
            IPage<Comment> page = commentService.pageListByUserId(pageNum, pageSize, queryMap);
            return ResponseData.success().putDataVule("commentList", page.getRecords()).putDataVule("pages", page.getPages()).putDataVule("total", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @PostMapping("/findList")
    @ApiOperation(value = "条件查找评论", notes = "条件查找数据库中的部分评论," +
            "infoId、content、status、informUserId、informContent、userId、likedCount" +
            " 排序使用orderby 例如orderby:comment_time，orderby:liked_count...")
    @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataTypeClass = Map.class)
    public ResponseData<Comment> findList(@RequestBody Map<String, String> queryMap) {
        try {
            List<Comment> commentList = commentService.findList(queryMap);
            return ResponseData.success().putDataVule("commentList", commentList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }



    @PostMapping("/toggleLike")
    @ApiOperation(value = "点赞或不点赞", notes = "to great or not great， that is a question")
    @ApiImplicitParam(name = "queryMap", value = "查询条件comId、userId 这两个必须有", required = true, paramType = "body", dataTypeClass = Map.class)
    public ResponseData<Comment> toggleLike(@RequestBody Map<String, String> queryMap) {
        try {
            commentService.toggleLike(queryMap);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }


    @PostMapping("/inform")
    @ApiOperation(value = "举报评论", notes = "举报资源帖，需要回复主键comId，举报人informUserId，举报原因informContent，缺一不可")
    @ApiImplicitParam(name = "informMap", value = "举报条件", required = true, paramType = "body", dataTypeClass = Map.class)
    public ResponseData<Comment> inform(@RequestBody Map<String, String> informMap) {
        try {
            commentService.inform(informMap);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }


    @GetMapping("/informSuccess")
    @ApiOperation(value = "举报评论成功", notes = "举报成功某一个评论，需要评论主键comId")
    @ApiImplicitParam(name = "comId", value = "举报条件", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Comment> informSuccess(@RequestParam Integer comId) {
        try {
            commentService.informSuccess(comId);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }



    @GetMapping("/informFail")
    @ApiOperation(value = "举报评论失败", notes = "举报失败某一个评论，需要评论主键comId")
    @ApiImplicitParam(name = "comId", value = "举报条件", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Comment> informFail(@RequestParam Integer comId) {
        try {
            commentService.informFail(comId);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

}
